using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContentOverContainer.Common;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers.Common;

namespace ContentOverContainer.SimpleSyntax
{
    /// <summary>
    /// Syntax rewriter transforming code containing multiplicity attributes to code without those attributes by applying the multiplicity types.
    /// This rewriter transforms syntax nodes annotated by the multiplicity attribute and build a list/stack with their reference to be adjusted too (see: ReferencesRewriter).
    /// It does not regard linq or lambda expression.
    /// </summary>
    /// <author>Harald Binkle</author>
    public class SimpleSyntaxMultiplicityRewriter : SyntaxRewriter
    {
        private List<ChangeNodeMethodSubstitution> m_MethodsToAdd;
        

        public SimpleSyntaxMultiplicityRewriter(List<ChangeNodeMethodSubstitution> pMethodsToAdd)
        {
            m_MethodsToAdd = pMethodsToAdd;        
        }

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            ClassDeclarationSyntax result = base.VisitClassDeclaration(node) as ClassDeclarationSyntax;

            foreach (var changeNodeMethodSubstitution in m_MethodsToAdd)
            {
                if (changeNodeMethodSubstitution.Class.Identifier.EquivalentTo(node.Identifier))
                    result = result.WithMembers(result.Members.Add(changeNodeMethodSubstitution.MethodDeclaration));
            }

            return result;
        }
    }
}
